package someTestExcemple.string;

import java.util.Scanner;

import static someTestExcemple.test2.crateVerificationCode;

//用户登录界面案例
public class StringTest1 {
    public static void main(String[] args) {
        login();
    }

    public static boolean loginCheck(String username, String password) {
        //2.定义正确用户名和密码
        String rightUsername = "admin";
        String rightPassword = "123456";
        //3.比较用户名和密码是否正确
        if (username.equals(rightUsername) && password.equals(rightPassword)) {
            System.out.println("登录成功");
            return true;
        } else {
            System.out.println("用户名或密码错误");
            return false;
        }
    }

    public static void login() {
        System.out.println("欢迎使用系统~~~~~");
        for (int i = 0; i < 3; i++) {
            //1.登录界面，用户输入用户名和密码
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            //生成随机6位验证码
            String verificationCode = crateVerificationCode(6);
            System.out.println("验证码为:" + verificationCode);
            System.out.println("请输入验证码:");
            String inputVerificationCode = sc.next();
            if ( verificationCode.equals(inputVerificationCode) && loginCheck(username, password)) {
                break;
            } else {
                if (i == 2) {
                    System.out.println("登录失败，账号被锁定，请12小时后再试!");
                } else {
                    System.out.println("登录失败，还有" + (2 - i) + "次机会");
                }
            }
        }
    }


}


